package com.imay.live.view.custom;

import android.animation.Animator;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.imay.live.R;
import com.imay.live.view.CountdownView;


/**
 * 直播挑战
 * Created by lzx on 2017/06/23 0023.
 */

public class ChallengeView extends RelativeLayout implements Animator.AnimatorListener {

    private TextView mTitleTv;
    private TextView mContentTv;
    private TextView mAuthorTv;
    private TextView mTipsTv;
    private LottieAnimationView mAnimationView;

    private CountDownTimerListener mListener;

    public ChallengeView(Context context) {
        super(context);
        init(context);
    }

    public ChallengeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ChallengeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public ChallengeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.view_challenge, this);
        mTitleTv = (TextView) findViewById(R.id.challenge_tv_title);
        mContentTv = (TextView) findViewById(R.id.challenge_tv_content);
        mAuthorTv = (TextView) findViewById(R.id.challenge_tv_author);
        mTipsTv = (TextView) findViewById(R.id.challenge_tv_tips);

        mAnimationView = (LottieAnimationView) findViewById(R.id.challenge_lav_time);
        mAnimationView.useExperimentalHardwareAcceleration();
        mAnimationView.addAnimatorListener(this);
        mAnimationView.playAnimation();
    }

    public void setTitle(String title) {
        if (mTitleTv != null) {
            SpannableString spanstring = new SpannableString("# " + title);
            spanstring.setSpan(new ForegroundColorSpan(getContext().getResources().getColor(R.color.text_23)), 0, 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            mTitleTv.setText(spanstring);
        }
    }

    public void setContent(String content) {
        if (mContentTv != null) {
            mContentTv.setText(content);
        }
    }

    public void setTips(String tips) {
        if (mTipsTv != null) {
            mTipsTv.setText(tips);
        }
    }

    public void setAuthor(String author) {
        if (mAuthorTv != null) {
            mAuthorTv.setText(getResources().getString(R.string.short_live_challenge_author, author));
        }
    }

    @Override
    public void onAnimationStart(Animator animation) {

    }

    @Override
    public void onAnimationEnd(Animator animation) {
        if (mListener != null) {
            mListener.onFinish();
        }
    }

    @Override
    public void onAnimationCancel(Animator animation) {

    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }

    public void setCountDownTimerListener(CountDownTimerListener listener) {
        this.mListener = listener;
    }

    public interface CountDownTimerListener {
        void onFinish();
    }
}
